package cn.itrip.service.itripHotelOrder;
import cn.itrip.beans.pojo.*;
import cn.itrip.beans.vo.order.ValidateRoomStoreVO;
import cn.itrip.common.BigDecimalUtil;
import cn.itrip.mapper.itripHotelOrder.ItripHotelOrderMapper;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.Page;
import cn.itrip.mapper.itripHotelRoom.ItripHotelRoomMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.itrip.common.Constants;
import sun.rmi.runtime.Log;

import static java.math.BigDecimal.ROUND_DOWN;

@Service
public class ItripHotelOrderServiceImpl implements ItripHotelOrderService {

    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;

    @Resource
    private ItripHotelRoomMapper itripHotelRoomMapper;

    public ItripHotelOrder getItripHotelOrderById(Long id)throws Exception{
        return itripHotelOrderMapper.getItripHotelOrderById(id);
    }

    public List<ItripHotelOrder>	getItripHotelOrderListByMap(Map<String,Object> param)throws Exception{
        return itripHotelOrderMapper.getItripHotelOrderListByMap(param);
    }

    public Integer getItripHotelOrderCountByMap(Map<String,Object> param)throws Exception{
        return itripHotelOrderMapper.getItripHotelOrderCountByMap(param);
    }

    public Integer itriptxAddItripHotelOrder(ItripHotelOrder itripHotelOrder)throws Exception{
            itripHotelOrder.setCreationDate(new Date());
            return itripHotelOrderMapper.insertItripHotelOrder(itripHotelOrder);
    }

    public Integer itriptxModifyItripHotelOrder(ItripHotelOrder itripHotelOrder)throws Exception{
        itripHotelOrder.setModifyDate(new Date());
        return itripHotelOrderMapper.updateItripHotelOrder(itripHotelOrder);
    }

    public Integer itriptxDeleteItripHotelOrderById(Long id)throws Exception{
        return itripHotelOrderMapper.deleteItripHotelOrderById(id);
    }

    public Page<ItripHotelOrder> queryItripHotelOrderPageByMap(Map<String,Object> param,Integer pageNo,Integer pageSize)throws Exception{
        Integer total = itripHotelOrderMapper.getItripHotelOrderCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<ItripHotelOrder> itripHotelOrderList = itripHotelOrderMapper.getItripHotelOrderListByMap(param);
        page.setRows(itripHotelOrderList);
        return page;
    }

    @Override
    public List<ItripHotelTempStore> queryRoomStore(Map<String, Object> param) {
        //调用存储过程 mapper,完善实时库存数据
        itripHotelOrderMapper.flushStore(param);

        //查询这段时间内,实时库存数据 mapper

        return itripHotelOrderMapper.queryRoomStore(param);
    }

    @Override
    public boolean valideroomcount(int count, Map<String, Object> param) {
        List<ItripHotelTempStore> list=itripHotelOrderMapper.queryRoomStore(param);
        return count<list.get(0).getStore();
    }

    @Override
    public BigDecimal payAmount(int count, long roomId) throws Exception {
        double price =itripHotelRoomMapper.getItripHotelRoomById(roomId).getRoomPrice();


        return BigDecimalUtil.OperationASMD(count,price, BigDecimalUtil.BigDecimalOprations.multiply,2,ROUND_DOWN);
    }


    public boolean isOffLinePaymentSupport(long roomId) throws Exception {
        ItripHotelRoom  itripHotelRoom=itripHotelRoomMapper.getItripHotelRoomById(roomId);
        int id =itripHotelRoom.getPayType();
        return (2&id)!=0;
    }

    @Override
    public void cancelOrderTimeOut() {
        itripHotelOrderMapper.cancelOrderTimeOut();
    }

    @Override
    public void changOrderToCommit() {
        itripHotelOrderMapper.changOrdertoCommit();
    }

    @Override
    public void itriptxFlushTempStore(List<ItripTradeEnds> list) throws Exception {
        for (ItripTradeEnds tradeEnd:list) {
            //根据订单人的入驻时间和退房时间，修改制定房间的实时库存
            ItripHotelOrder itripHotelOrder =itripHotelOrderMapper.getItripHotelOrderById(tradeEnd.getId());
            this.flushTempStore(itripHotelOrder);


        }
    }


    public void flushTempStore(ItripHotelOrder order){
        Map<String,Object> params=new HashMap<>();
        params.put("count",order.getCount());
        params.put("roomId",order.getRoomId());
        params.put("startDate",order.getCheckInDate());
        params.put("endDate",order.getCheckOutDate());
         itripHotelOrderMapper.flushTempStore(params);
    }

}
